package com.hbmcc.heshenghuo.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.hbmcc.heshenghuo.R
import android.graphics.Bitmap
import android.util.Log

/**
 * https://www.jb51.net/article/148527.htm
 * 圆盘计步图表
 */
class CircleProgressBar : View {

    /**
     * 外部的空心圆的画笔
     */
    private val  outWheelPaint:Paint = Paint()
    /**
     * 内部的空心圆的画笔
     */
    private val progressPaint:Paint = Paint()
    /**
     * 外部圆的外切正方形
     */
    private val outWheel:RectF = RectF()

    /**
     * 内部圆的外切正方形
     */
    private val inWheel:RectF = RectF()

    private var bitmap:Bitmap? =null

    private var bitMatrix: Matrix =  Matrix()

    private val bitPaint:Paint = Paint()

    constructor(context: Context?) : super(context) {
        init(null, 0)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs, 0)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
        init(attrs, defStyle)
    }

    private fun init(attrs: AttributeSet?, defStyle: Int) {



    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawBitmap(bitmap!!,bitMatrix,bitPaint)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val height = getDefaultSize(suggestedMinimumHeight, heightMeasureSpec)
        val width = getDefaultSize(suggestedMinimumWidth, widthMeasureSpec)
        outWheel.left = 0F
        outWheel.top = 0F
        outWheel.bottom = width.coerceAtMost(height).toFloat()
        outWheel.right = outWheel.bottom
        setMeasuredDimension( outWheel.bottom.toInt(),  outWheel.bottom.toInt())

        val space = context.resources.getDimension(R.dimen.dp_4)
        inWheel.left = space
        inWheel.top = space
        inWheel.bottom = width.coerceAtMost(height).toFloat() -space
        inWheel.right = inWheel.bottom

        outWheelPaint.color =Color.parseColor("#FFCDE6FF")
        outWheelPaint.style = Paint.Style.STROKE // 实心
        outWheelPaint.strokeCap = Paint.Cap.ROUND // 圆角画笔
        outWheelPaint.isAntiAlias = true // 去锯齿
        outWheelPaint.strokeWidth = context.resources.getDimension(R.dimen.dp_6)

        progressPaint.shader = SweepGradient(outWheel.right/2,outWheel.right/2, Color.parseColor("#FF029E7D"),Color.parseColor("#FF9BE37B"))
        progressPaint.style = Paint.Style.STROKE // 实心
        progressPaint.strokeCap = Paint.Cap.ROUND // 圆角画笔
        progressPaint.isAntiAlias = true // 去锯齿
        progressPaint.strokeWidth = context.resources.getDimension(R.dimen.dp_6)

        initBitmap(height,width)
    }

    private fun initBitmap(height:Int,width:Int){
        val bitmap = Bitmap.createBitmap(height, width, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(bitmap)

        canvas.drawArc(inWheel, 0f, 359f, false, outWheelPaint)
        canvas.drawArc(inWheel, 10f, 260f, false, progressPaint)
        canvas.save()
        canvas.restore()

        val m = Matrix()
        m.setRotate(270F, height.toFloat(),  width.toFloat())

        this.bitmap =  Bitmap.createBitmap(bitmap, 0, 0, height,  width, m, false)
    }


}